package com.ruoyi.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.TemplateData;
import com.ruoyi.system.domain.TemplateTableRel;
import com.ruoyi.system.service.ITemplateDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TemplateFieldDataMapper;
import com.ruoyi.system.domain.TemplateFieldData;
import com.ruoyi.system.service.ITemplateFieldDataService;

import javax.annotation.Resource;

/**
 * 业务模板字段Service业务层处理
 *
 * @author chenming
 * @date 2024-10-16
 */
@Service
public class TemplateFieldDataServiceImpl extends ServiceImpl<TemplateFieldDataMapper, TemplateFieldData> implements ITemplateFieldDataService
{
    @Resource
    private TemplateFieldDataMapper templateFieldDataMapper;

    @Resource
    private ITemplateDataService templateDataService;

    @Resource
    private TemplateTableRelServiceImpl templateTableRelService;

    /**
     * 查询业务模板字段
     *
     * @param fieldId 业务模板字段主键
     * @return 业务模板字段
     */
    @Override
    public TemplateFieldData selectTemplateFieldDataByFieldId(Long fieldId)
    {
        return templateFieldDataMapper.selectTemplateFieldDataByFieldId(fieldId);
    }

    /**
     * 查询业务模板字段列表
     *
     * @param templateFieldData 业务模板字段
     * @return 业务模板字段
     */
    @Override
    public List<TemplateFieldData> selectTemplateFieldDataList(TemplateFieldData templateFieldData)
    {
        return templateFieldDataMapper.selectTemplateFieldDataList(templateFieldData);
    }

    /**
     * 新增业务模板字段
     *
     * @param templateFieldData 业务模板字段
     * @return 结果
     */
    @Override
    public int insertTemplateFieldData(TemplateFieldData templateFieldData)
    {
        return templateFieldDataMapper.insertTemplateFieldData(templateFieldData);
    }

    /**
     * 修改业务模板字段
     *
     * @param templateFieldData 业务模板字段
     * @return 结果
     */
    @Override
    public int updateTemplateFieldData(TemplateFieldData templateFieldData)
    {
        return templateFieldDataMapper.updateTemplateFieldData(templateFieldData);
    }

    /**
     * 批量删除业务模板字段
     *
     * @param fieldIds 需要删除的业务模板字段主键
     * @return 结果
     */
    @Override
    public int deleteTemplateFieldDataByFieldIds(Long[] fieldIds)
    {
        // 删除字段需要重新关联表
        TemplateFieldData templateFieldData = templateFieldDataMapper.selectTemplateFieldDataByFieldId(fieldIds[0]);
        int i = templateFieldDataMapper.deleteTemplateFieldDataByFieldIds(fieldIds);
        LambdaUpdateWrapper<TemplateData> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(TemplateData::getTableId,null);
        updateWrapper.eq(TemplateData::getTemplateId,templateFieldData.getTemplateId());
        templateDataService.update(updateWrapper);

        // 删除关联字段表
        templateTableRelService.remove(new LambdaQueryWrapper<TemplateTableRel>().in(TemplateTableRel::getTemplateFieldId, fieldIds).eq(TemplateTableRel::getTemplateId, templateFieldData.getTemplateId()));
        return i;
    }

    /**
     * 删除业务模板字段信息
     *
     * @param fieldId 业务模板字段主键
     * @return 结果
     */
    @Override
    public int deleteTemplateFieldDataByFieldId(Long fieldId)
    {
        TemplateFieldData templateFieldData = templateFieldDataMapper.selectTemplateFieldDataByFieldId(fieldId);
        LambdaUpdateWrapper<TemplateData> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(TemplateData::getTableId,null);
        updateWrapper.eq(TemplateData::getTemplateId,templateFieldData.getTemplateId());
        templateDataService.update(updateWrapper);
        // 删除关联字段表
        templateTableRelService.remove(new LambdaQueryWrapper<TemplateTableRel>().eq(TemplateTableRel::getTemplateFieldId, fieldId).eq(TemplateTableRel::getTemplateId, templateFieldData.getTemplateId()));
        return templateFieldDataMapper.deleteTemplateFieldDataByFieldId(fieldId);
    }
}
